package com.my.xinyidaixi.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.my.xinyidaixi.Activity.ZongHe_WebView;
import com.my.xinyidaixi.DataBase.Bean.Advlist;
import com.my.xinyidaixi.DataBase.Model.Over_UserModer;
import com.my.xinyidaixi.DataBase.Model.UserModel;
import com.my.xinyidaixi.Globe.MyApplication;
import com.my.xinyidaixi.Util.CacheSave;

import java.util.ArrayList;

/**
 * Created by zw on 2017/6/3.
 */
public class RollViewAdapter extends StaticPagerAdapter{

    private UserModel model=new Over_UserModer();
    private CacheSave cacheSave=new CacheSave();
    private Context context= MyApplication.getInstance();
    //private int [] imgs={R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};
    private String [] url=new String [3]; //必须初始化才能使用 动态添加的话 new String [X.size()]
    private String [] adv_url=new String[3];
    //首页图片滚动适配器
    @Override
    public View getView(ViewGroup container, final int position) {
        ArrayList<Advlist> item=model.get_advlist();
        ImageView view =new ImageView(container.getContext());
        for(int i=0;i<item.size();i++)
        {
            if(item.get(i).getAdv_id().equals("1"))
            {
                url[0]=item.get(i).getAdv_img_url(); //地址url回头再获取
                adv_url[0]=item.get(i).getAdv_url();
                cacheSave.finalBitmap(view,url[0]);
            }
            if (item.get(i).getAdv_id().equals("2"))
            {
                url[1] = item.get(i).getAdv_img_url();
                adv_url[1]=item.get(i).getAdv_url();
                cacheSave.finalBitmap(view,url[1]);
            }
            if (item.get(i).getAdv_id().equals("3")) {
                url[2] = item.get(i).getAdv_img_url();
                adv_url[2]=item.get(i).getAdv_url();
                cacheSave.finalBitmap(view,url[2]);
            }
        }

        view.setScaleType(ImageView.ScaleType.FIT_XY);
        // 按比例扩大图片的size居中显示，使得图片长(宽)等于或大于View的长(宽)
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Toast.makeText(context,"点击了第"+position+"图片",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent();
                intent.setClass(context, ZongHe_WebView.class);
                intent.putExtra("url",adv_url[position]);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
        return view;
    }
    @Override
    public int getCount() {
        return  url.length;
    }
}
